LIKE '[!agm]%'

Hi,

I have created a Customer table with Country column and inserted 4 rows with country names (Germany, Mexico and Angola)

When I execute the following query

        SELECT *FROMCustomersWHERECountryLIKE'[!agm]%'

result set is showing all 4 rows but I read like the above query should

select all customers with a Country NOT starting  with "a", "g", or "m":

Please help me

correct me if I am wrong.

Thanks,

 

February 3rd, 2014 3:29am

Vinodh,

Check if the following helps you:

declare @tab table (country varchar(100))
insert @tab select 'Angola'
insert @tab select 'Mexico'
insert @tab select 'Germany'
insert @tab select 'India'
insert @tab select 'Australia'

--Not starting with a,g,m
--method 1
select country
from @tab
where country like '[^agm]%'

--method 2
select country
from @tab
where country not like '[agm]%'

--starting with a,g,m
select country
from @tab
where country like '[agm]%'

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2014 3:38am

I think you are looking for the below:

create table Customers1(Country varchar(100))
Insert into Customers1 values ('Germany'),('Mexico'),('Angola'),('USA'),('India'),('Australia')


SELECT * FROM Customers1 WHERE Country LIKE'[^agm]%'

Drop table Customers1

February 3rd, 2014 3:39am

Hi Jay,

Thanks, Your reply is working fine. Please tell me what's wrong in my query.

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2014 3:43am

Hi Vinodh

You should use ^ instead !

Below will work for you:

 SELECT * FROM Customers WHERE Country LIKE '[^agm]%'

I am not sure where you read about "!" , if you have please share that

February 3rd, 2014 3:46am

Hi Jay,

Thanks,

SELECT *FROMCustomersWHERECountryLIKE'[^agm]%'

is working fine.

 

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2014 3:46am

The below notation is wrong:

  SELECT *FROMCustomersWHERECountryLIKE'[!agm]%'

[!] character wont do the intended functionality, rather you should use [^] character.

February 3rd, 2014 3:47am

Vinodh,

Sorry for the late reply. Yes, as Latheesh and Saurabh pointed out, usage of '^' instead of '!' is the corrective action. We dont use '!' in cas eof wildcard specifications..

Great to know tht the query is working! Cheers!

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2014 4:22am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics